using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ns18;
using ns20;

namespace ns16
{
	internal abstract class Class136
	{
		private class Class139 : IEqualityComparer<object>
		{
			bool IEqualityComparer<object>.Equals(object x, object y)
			{
				return object.ReferenceEquals(x, y);
			}

			int IEqualityComparer<object>.GetHashCode(object obj)
			{
				return RuntimeHelpers.GetHashCode(obj);
			}
		}

		private ErrorContext errorContext_0;

		private Class180<string, object> class180_0;

		private bool bool_0;

		internal readonly JsonSerializer jsonSerializer_0;

		internal readonly ITraceWriter itraceWriter_0;

		internal Class180<string, object> Class180_0
		{
			get
			{
				if (class180_0 == null)
				{
					class180_0 = new Class180<string, object>(EqualityComparer<string>.Default, new Class139(), "A different value already has the Id '{0}'.", "A different Id has already been assigned for value '{0}'.");
				}
				return class180_0;
			}
		}

		protected Class136(JsonSerializer serializer)
		{
			Class204.smethod_2(serializer, "serializer");
			jsonSerializer_0 = serializer;
			itraceWriter_0 = serializer.ITraceWriter_0;
			bool_0 = GetType() == typeof(Class138);
		}

		private ErrorContext method_0(object object_0, object object_1, string string_0, Exception exception_0)
		{
			if (errorContext_0 == null)
			{
				errorContext_0 = new ErrorContext(object_0, object_1, string_0, exception_0);
			}
			if (errorContext_0.Error != exception_0)
			{
				throw new InvalidOperationException("Current error context error is different to requested error.");
			}
			return errorContext_0;
		}

		protected void method_1()
		{
			if (errorContext_0 == null)
			{
				throw new InvalidOperationException("Could not clear error context. Error context is already null.");
			}
			errorContext_0 = null;
		}

		protected bool method_2(object object_0, JsonContract jsonContract_0, object object_1, IJsonLineInfo ijsonLineInfo_0, string string_0, Exception exception_0)
		{
			ErrorContext errorContext = method_0(object_0, object_1, string_0, exception_0);
			if (itraceWriter_0 != null && itraceWriter_0.LevelFilter >= TraceLevel.Error && !errorContext.Boolean_0)
			{
				errorContext.Boolean_0 = true;
				string text = (bool_0 ? "Error serializing" : "Error deserializing");
				if (jsonContract_0 != null)
				{
					text = text + " " + jsonContract_0.UnderlyingType;
				}
				text = text + ". " + exception_0.Message;
				if (!(exception_0 is JsonException))
				{
					text = Struct25.smethod_2(ijsonLineInfo_0, string_0, text);
				}
				itraceWriter_0.Trace(TraceLevel.Error, text, exception_0);
			}
			jsonContract_0?.method_4(object_0, jsonSerializer_0.StreamingContext_0, errorContext);
			if (!errorContext.Handled)
			{
				jsonSerializer_0.method_3(new ErrorEventArgs(object_0, errorContext));
			}
			return errorContext.Handled;
		}
	}
}
